/*
 * Copyright (C) 2017 Jared Rummler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaredrummler.ohos.colorpicker.demo;

import com.jaredrummler.ohos.colorpicker.ColorPanelView;
import com.jaredrummler.ohos.colorpicker.ColorPickerView;
import com.jaredrummler.ohos.colorpicker.utils.Log;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.media.image.common.PixelFormat;


public class ColorPickerActivity extends FractionAbility implements ColorPickerView.OnColorChangedListener, Component.ClickedListener {

  private ColorPickerView colorPickerView;
  private ColorPanelView newColorPanelView;

  @Override public void onStart(Intent intent) {
    super.onStart(intent);
    getWindow().setPixelFormat(PixelFormat.ARGB_8888.getValue());

    setUIContent(ResourceTable.Layout_activity_color_picker);

    DatabaseHelper helper = new DatabaseHelper(this);
    Preferences prefs = helper.getPreferences("preferences");
    int initialColor = prefs.getInt("color_3", 0xFF000000);
    Log.i("Color", "onStart " + initialColor);

    colorPickerView = (ColorPickerView) findComponentById(ResourceTable.Id_cpv_color_picker_view);
    ColorPanelView colorPanelView = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_old);
    newColorPanelView = (ColorPanelView) findComponentById(ResourceTable.Id_cpv_color_panel_new);

    Button btnOK = (Button) findComponentById(ResourceTable.Id_okButton);
    Button btnCancel = (Button) findComponentById(ResourceTable.Id_cancelButton);

    ((DirectionalLayout) colorPanelView.getComponentParent()).setPadding(colorPickerView.getPaddingLeft(), 0,
        colorPickerView.getPaddingRight(), 0);

    colorPickerView.setOnColorChangedListener(this);
    colorPickerView.setColor(initialColor, true);
    colorPanelView.setColor(initialColor);

    btnOK.setClickedListener(this);
    btnCancel.setClickedListener(this);
  }

  @Override public void onColorChanged(int newColor) {
    Log.i("Color", "onColorChanged " + newColor);
    newColorPanelView.setColor(newColor);
  }

  @Override
  public void onClick(Component v) {
    switch (v.getId()) {
      case ResourceTable.Id_okButton:
        DatabaseHelper helper = new DatabaseHelper(this);
        Preferences prefs = helper.getPreferences("preferences");
        prefs.putInt("color_3", colorPickerView.getColor());
        Log.i("Color", "save " + colorPickerView.getColor());
        prefs.flushSync();
        terminateAbility();
        break;
      case ResourceTable.Id_cancelButton:
        terminateAbility();
        break;
    }
  }
}
